﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using ManagementEffect;
using ManagementEffect.Engine;
using Common;

namespace ServerYuGi.PythonRegister
{
    public class PYRegisterManager
    {
        SSupervisor super;
        SCard CardRegisterNow;
        CardScript CardScriptNow;
        DonDangKi DonDangKiNow;
        StoreCheckRegister Store;
        public PYRegisterManager(SSupervisor super)
        {
            this.super = super;
            Store = new StoreCheckRegister();
            Init();
        }
        public void Init()
        {
            CardRegisterNow = null;
            CardScriptNow = null;
            DonDangKiNow = null;
        }   
        public void F5()
        {
            Init();
            Store.F5();
        }

        public void SetStore(SCard CardRegisterNow,CardScript CardScriptNow,DonDangKi DonDangKiNow)
        {
            this.CardRegisterNow = CardRegisterNow;
            this.CardScriptNow = CardScriptNow;
            this.DonDangKiNow = DonDangKiNow;
        }

        public void CheckDangKyScript(SCard card)
        {
            try
            {                
                CardScript XMLCardScript = super.CScriptDB.GetScript(card.MyYBaseCard.Card_TypeID);
                if (XMLCardScript == null)
                    return;
                if(!CheckTypeReCheck(XMLCardScript,card))
                {                    
                    Store.AddCardID(XMLCardScript.GetListCheckPoint(), card.MyYBaseCard.ID);
                    return;
                }
                
                CardScriptNow = XMLCardScript;
                CardRegisterNow = card;
                DonDangKi tmp = new DonDangKi(super.EffectTurnManager, card.MyYBaseCard.ID);
                
                DonDangKiNow = tmp;
                string source;
                if (XMLCardScript.IsCheck_Hand && isOnHand(card))
                    source = XMLCardScript.Check_Register_Script_OnHand;                    
                else
                    source = XMLCardScript.Check_Register_Script;
                    
                List<string> names = new List<string>() { "dondangky", "api_manager" };                  
                super.engine.SetVariable(names.ToArray(), tmp,super.api_manager);
                super.engine.SetCode(source);               
                super.engine.Excute();   
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }

        //  [4/23/2010 TinNguyen]
        /// <summary>
        /// Xai De Check DK Script sau khi chia bai xong ( Khong phan biet Recheck Type)
        /// </summary>
        /// <param name="card"></param>
        public void CheckDangKyScriptBatDauVan(SCard card)
        {
            try
            {
                
                CardScript XMLCardScript = super.CScriptDB.GetScript(card.MyYBaseCard.Card_TypeID);
                if (XMLCardScript == null)
                    return;
               

                CardScriptNow = XMLCardScript;
                CardRegisterNow = card;
                DonDangKi tmp = new DonDangKi(super.EffectTurnManager, card.MyYBaseCard.ID);

                DonDangKiNow = tmp;
                string source;
                if (XMLCardScript.IsCheck_Hand && isOnHand(card))
                    source = XMLCardScript.Check_Register_Script_OnHand;
                else
                    source = XMLCardScript.Check_Register_Script;

                List<string> names = new List<string>() { "dondangky", "api_manager" };
                super.engine.SetVariable(names.ToArray(), tmp, super.api_manager);
                super.engine.SetCode(source);
                super.engine.Excute();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }
        }
        bool CheckTypeReCheck(CardScript script,SCard card)
        {
            SPlayer playerOwner = super.LayPlayer(super.TurnManager.IDPlayerTrongLuot);
            if (playerOwner.CheckIDInAll(card.MyYBaseCard.ID))
            {
                if (script.TypeReCheck == TypeCheck.Both || script.TypeReCheck == TypeCheck.MyTurn)
                    return true;
                else
                    return false;
            }
            playerOwner = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            if (playerOwner.CheckIDInAll(card.MyYBaseCard.ID))
            {
                if (script.TypeReCheck == TypeCheck.Both || script.TypeReCheck == TypeCheck.OtherTurn)
                    return true;
                else
                    return false;
            }
            return false;
        }
        public void DangKyScript(bool result, byte ID)
        {
            if (ID != CardRegisterNow.MyYBaseCard.ID)
            {
                Console.WriteLine("ID trong Script Check Dang Ki Khac voi ID cua la bai dang xet");
                return;
            }
            List<YCheckPoint> Checkpoints = CardScriptNow.GetListCheckPoint();
            if (result == false)
            {
                Init();

                //-------------------Them la bai chua dang ki duoc vao Store -----------------------------------//
                Store.AddCardID(Checkpoints, ID);
                //----------------------------------------------------------------------------------------------//

                Console.WriteLine("Chua du dieu kien de dang ki Script");
                return;
            }

            Console.WriteLine("----Dieu kien hop le----");
            //------------- Tim Xoa La Bai trong Store -------------//
            Store.RemoveCardID(Checkpoints, ID);
            //-----------------------------------------------------//
            

            string source = CardScriptNow.Register_Script;
            List<string> names = new List<string>() { "dondangky", "api_manager" };
            super.engine.SetVariable(names.ToArray(), DonDangKiNow, super.api_manager);
            super.engine.SetCode(source);
            Console.WriteLine("---------------------------------------------------------");
            super.engine.Excute();
            Console.WriteLine("---------------------------------------------------------");
            Init();
        }        
        public void ReCheckDangKyScript(YCheckPoint checkpoint)
        {
            List<int> ListID =new List<int>();
            ListID.AddRange(Store.GetListID(checkpoint));
            foreach (int id in ListID)
            {
                ReCheckWithID(id);
            }
        }
        void ReCheckWithID(int ID)
        {
            SPlayer player = super.LayPlayer(super.TurnManager.IDPlayerTrongLuot);
            SPlayer Other = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            SCard card;
            if(player.CheckIDCardTrenBan(ID) || player.CheckIDCardTrenTay(ID))
            {
                card = player.LayCardInAll(ID);
            }
            else
            {
                card = Other.LayCardInAll(ID);
            }
            if (card == null)
            {
                Console.WriteLine("Khong lay duoc trong Store gia tri tuong ung voi ID = {0}",ID);
                return;
            }
            
            //Store.RemoveCardID(super.CScriptDB.DatabaseScript[card.MyYBaseCard.Card_TypeID].GetListCheckPoint(), ID);
            CheckDangKyScript(card);
        }


        public void RemoveIDinStore(SCard card)
        {
            CardScript cardscript = super.CScriptDB.GetScript(card.MyYBaseCard.Card_TypeID);
            if(cardscript==null)
                return;
            Store.RemoveCardID(cardscript.GetListCheckPoint(),card.MyYBaseCard.ID);
        }


        //-------------------cung cap cho API---------------------//
        public void AddStore(List<YCheckPoint> checkpoint,int ID)
        {
            Store.AddCardID(checkpoint, ID);
        }
        bool isOnHand(SCard card)
        {
            SPlayer player = super.LayPlayerKhacVoiPlayerDangTrongLuot();
            if (player.CheckIDCardTrenTay(card.MyYBaseCard.ID))
                return true;
            player = super.LayPlayerKhacVoiID(player.ID);
            if (player.CheckIDCardTrenTay(card.MyYBaseCard.ID))
                return true;
            return false;
        }




    }
}
